package demo.practice.uf;

public class P1319 {


    public static void main(String[] args) {
        P1319 p1319 = new P1319();
        int i = p1319.makeConnected(6, new int[][]{{0, 1}, {0, 2}, {0, 3}, {1, 2}});
        System.out.println(i);
    }

    public int makeConnected(int n, int[][] connections) {
        if (connections == null || n < 1 || n - 1 != connections.length)
            return -1;

        UFRankUnionPlant uf = new UFRankUnionPlant(n);
        for (int[] c : connections) {
            uf.join(c[0], c[1]);
        }

        return uf.plant - 1;
    }
}
